/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:28 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class Khoahoc_BaihocBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Khoahoc_Baihoc Này";
		private static string EX_PK_IKHOAHOC_BAIHOCID_INVALID="PK_iKhoahoc_BaihocID không hợp lệ";
		private static string EX_FK_IKHOAHOCID_INVALID="FK_iKhoahocID không hợp lệ";
		private static string EX_FK_IBAIHOCID_INVALID="FK_iBaihocID không hợp lệ";
		private static string EX_ID_INVALID="PK_iKhoahoc_BaihocID không hợp lệ";
        private static string EX_FIELD_EXISTED = "Bài học này đã có trong khóa học!";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Khoahoc_Baihoc Theo ID
        /// </summary>
        /// <param name="PK_iKhoahoc_BaihocID">Int64:Khoahoc_Baihoc ID</param>
        /// <returns>Khoahoc_BaihocEntity</returns>        
        public static Khoahoc_BaihocEntity GetOne(Int64 PK_iKhoahoc_BaihocID)
        {
            
			if(PK_iKhoahoc_BaihocID<=0)
				throw new Exception(EX_ID_INVALID);
            return Khoahoc_BaihocDAL.GetOne(PK_iKhoahoc_BaihocID);
        }
        /// <summary>
        /// Lấy về List các Khoahoc_Baihoc
        /// </summary>
        /// <returns>List Khoahoc_BaihocEntity:List Khoahoc_Baihoc Cần lấy</returns>
        public static List<Khoahoc_BaihocEntity> GetAll()
        {
            return Khoahoc_BaihocDAL.GetAll();
        }
        public static List<Khoahoc_BaihocEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
		{
			if(FK_iKhoahocID<=0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			return Khoahoc_BaihocDAL.GetByFK_iKhoahocID(FK_iKhoahocID);
		}public static List<Khoahoc_BaihocEntity> GetByFK_iBaihocID(Int64 FK_iBaihocID)
		{
			if(FK_iBaihocID<=0)
				throw new Exception(EX_FK_IBAIHOCID_INVALID);
			return Khoahoc_BaihocDAL.GetByFK_iBaihocID(FK_iBaihocID);
		}
        public static List<Khoahoc_BaihocEntity> GetByFK_iKhoahocIDAndFK_iBaihocID(Int64 FK_iKhoahocID, Int64 FK_iBaihocID)
        {
            if (FK_iKhoahocID <= 0)
                throw new Exception(EX_FK_IKHOAHOCID_INVALID);
            if (FK_iBaihocID <= 0)
                throw new Exception(EX_FK_IBAIHOCID_INVALID);
            return Khoahoc_BaihocDAL.GetByFK_iKhoahocIDAndFK_iBaihocID(FK_iKhoahocID, FK_iBaihocID);
        }
        /// <summary>
        /// Kiểm tra và thêm mới Khoahoc_Baihoc
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Khoahoc_Baihoc Mới Thêm Vào</returns>
        public static Int32 Add(Khoahoc_BaihocEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return Khoahoc_BaihocDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Khoahoc_Baihoc
        /// </summary>
        /// <param name="entity">Khoahoc_BaihocEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(Khoahoc_BaihocEntity entity)
        {
            checkExist(entity.PK_iKhoahoc_BaihocID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return Khoahoc_BaihocDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Khoahoc_Baihoc
        /// </summary>
        /// <param name="PK_iKhoahoc_BaihocID">Int64 : PK_iKhoahoc_BaihocID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iKhoahoc_BaihocID)
        {
            checkExist(PK_iKhoahoc_BaihocID);
            return Khoahoc_BaihocDAL.Remove(PK_iKhoahoc_BaihocID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iKhoahoc_BaihocID)
        {
            Khoahoc_BaihocEntity oKhoahoc_Baihoc=Khoahoc_BaihocDAL.GetOne(PK_iKhoahoc_BaihocID);
            if(oKhoahoc_Baihoc==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">Khoahoc_BaihocEntity: entity</param>
        private static void checkLogic(Khoahoc_BaihocEntity entity)
        {   
			if (entity.PK_iKhoahoc_BaihocID < 0)
				throw new Exception(EX_PK_IKHOAHOC_BAIHOCID_INVALID);
			if (entity.FK_iKhoahocID < 0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			if (entity.FK_iBaihocID < 0)
				throw new Exception(EX_FK_IBAIHOCID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">Khoahoc_BaihocEntity: Khoahoc_BaihocEntity</param>
        private static void checkDuplicate(Khoahoc_BaihocEntity entity,bool checkPK)
        {
            List<Khoahoc_BaihocEntity> list = Khoahoc_BaihocDAL.GetAll();
            if (list.Exists(
                delegate(Khoahoc_BaihocEntity oldEntity)
                {
                    bool result = (oldEntity.FK_iBaihocID == entity.FK_iBaihocID) && (oldEntity.FK_iKhoahocID == entity.FK_iKhoahocID);
                    if(checkPK)
                        result=result && oldEntity.PK_iKhoahoc_BaihocID != entity.PK_iKhoahoc_BaihocID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">Khoahoc_BaihocEntity:entity</param>
        private static void checkFK(Khoahoc_BaihocEntity entity)
        {            
			KhoahocEntity oKhoahoc = KhoahocDAL.GetOne(entity.FK_iKhoahocID);
			if (oKhoahoc==null)
			{
				throw new Exception("Không tìm thấy :FK_iKhoahocID");
			}
			BaihocEntity oBaihoc = BaihocDAL.GetOne(entity.FK_iBaihocID);
			if (oBaihoc==null)
			{
				throw new Exception("Không tìm thấy :FK_iBaihocID");
			}
        }
        #endregion
    }
}
